package com.example.whac_a_mole;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

public class Activity_game extends AppCompatActivity {
    private static final String TAG = "LSK2";
    private int time=0;
    private Button[] buttons=new Button[9];
    private TextView edit_time;
    private TextView edit_mark;
    private Handler handler;
    private Random rd;
    private String user="test";//此处做测试，由Activity传入
    private int count=0;
    private int count_time=0;
    private int sum_error=0;//点错三次将退出游戏
    private DataBase database=new DataBase(Activity_game.this);
//    private SharedPreferences preferences;
//    private SharedPreferences.Editor editor;
//    private Set<String> top;
//    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        rd=new Random();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        time=getIntent().getIntExtra("time",1600);
        user=getIntent().getStringExtra("user");
       /* switch ((int) time)
        {
            case 1600:
                setTitle("打地鼠-初级模式");
                break;
            case 800:
                setTitle("打地鼠-中级模式");
                break;
            case 400:
                setTitle("打地鼠-高级模式");
                break;
            default:
                break;
        }*/
        //映射关系
        @SuppressLint("UseSparseArrays")
        final Map<Integer,String> map=new HashMap<>();
        map.put(1600,"初级模式");
        map.put(800,"中级模式");
        map.put(400,"高级模式");
        setTitle("打地鼠-"+map.get(time));

//        preferences=getSharedPreferences("top",MODE_PRIVATE);
//        editor=preferences.edit();
//        top = preferences.getStringSet("top",new HashSet<String>());
//        Log.i(TAG, top.toString());

        edit_time=findViewById(R.id.text_time);
        edit_mark=findViewById(R.id.text_mark);
        buttons[0]=findViewById(R.id.button1);
        buttons[1]=findViewById(R.id.button2);
        buttons[2]=findViewById(R.id.button3);
        buttons[3]=findViewById(R.id.button4);
        buttons[4]=findViewById(R.id.button5);
        buttons[5]=findViewById(R.id.button6);
        buttons[6]=findViewById(R.id.button7);
        buttons[7]=findViewById(R.id.button8);
        buttons[8]=findViewById(R.id.button9);
        //Log.i(TAG,Arrays.toString(buttons));

        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==0x1998)
                {
                    int t=rd.nextInt(9);
                    buttons[t].setBackgroundResource(R.drawable.mouse);
                    buttons[t].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            edit_mark.setText("得分:"+String.valueOf(++count));
                            Toast.makeText(Activity_game.this,"+1",Toast.LENGTH_LONG).show();
                        }
                    });
                    for (int i = 0; i < buttons.length; i++) {
                        if(i!=t) {
                            buttons[i].setBackgroundResource(R.drawable.bg);
                            buttons[i].setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Log.i(TAG, "onClick: 点错位置了,地鼠不在这里");
                                    sum_error++;
                                    if(sum_error<3) {
                                        AlertDialog.Builder builder = new AlertDialog.Builder(Activity_game.this).setTitle("是否结束游戏")
                                                .setMessage("您已经点错了" + String.valueOf(sum_error) + "次了(超过三次游戏结束)");
                                        builder.setPositiveButton("继续游戏", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                //什么都不做
                                            }
                                        });
                                        builder.setNegativeButton("重新开始", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                database.insert(map.get(time),user,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),count,count_time);
                                                Activity_game.this.recreate();


                                                //格式:模式+用户名+时间+分数
                                                //String str=map.get(time)+"@用户:admin@"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"@"+edit_mark.getText()+"@"+edit_time.getText();

                                                //startActivity(new Intent(Activity_game.this, Activity_game.class));
                                            }
                                        }).create().show();
                                    }
                                    else
                                    {
                                        AlertDialog.Builder builder = new AlertDialog.Builder(Activity_game.this).setTitle("游戏结束")
                                                .setMessage("您已经点错了"+String.valueOf(sum_error) +" 次了！游戏已经结束，请重新开始，如有疑惑请联系开发者！");
                                        builder.setPositiveButton("退出游戏", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                //退出
                                                //格式:模式+用户名+时间+分数
                                               // String str=map.get(time)+"@用户:admin@"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"@"+edit_mark.getText()+"@"+edit_time.getText();
                                                database.insert(map.get(time),user,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),count,count_time);

                                                Activity_game.this.finish();
                                            }
                                        });
                                        builder.setNegativeButton("重新开始", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                //格式:模式+用户名+时间+分数
                                                //String str=map.get(time)+"@用户:admin@"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"@"+edit_mark.getText()+"@"+edit_time.getText();
                                                database.insert(map.get(time),user,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),count,count_time);
                                                Activity_game.this.recreate();
                                                //startActivity(new Intent(Activity_game.this, Activity_game.class));
                                                //startActivity(new Intent(Activity_game.this, Activity_game.class));
                                            }
                                        }).create().show();
                                    }
                                    //重新加载
                                    //startActivity(new Intent(Activity_game.this,Activity_game.class));
                                }
                            });
                        }
                    }
                }
                if(msg.what==0x123)
                {
                    edit_time.setText("时间:"+String.valueOf(++count_time));
                }
                super.handleMessage(msg);
            }
        };

        //buttons[0].setBackgroundResource(R.drawable.mouse);
       /*
        HashSet<Integer> set=new HashSet<>();
        for (int i = 0; i < 1000; i++) {
            set.add(rd.nextInt(9));
        }
        */
        //System.out.println(set);
        /*
        for (Button button : buttons) {
            button.setBackgroundResource(R.drawable.mouse);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(Activity_game.this,"点击了",Toast.LENGTH_LONG).show();
                }
            });
        }*/

        /*
        String data = getIntent().getStringExtra("data");
        assert data != null;
        Log.i(TAG, data);*/
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
               handler.sendEmptyMessage(0x1998);
            }
        },0,time);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x123);
            }
        },0,1000);



    }
}
